import {message} from 'antd';
import {
	fakeSubmitArticle,
	queryArticle,
	queryArticleList,
	subCheck,
	check,
	pubArticle,
	cancelPubArticle
} from '../services/article';
import {queryAuthor} from '../services/author';

export default {
	namespace: 'article',

	state: {
		list: [],
		pagination: [],
		page: 0,
		authors: [],
		step: {
			id: '',
			author: '',
			author_id: '',
			intro: '',
			title: '',
			content: '',
			cover: '',
            recommend:0,
            recommend_cover:'',
		},
	},

	effects: {
		*fetchAuthor({
			payload
		}, {call, put}) {
			const response = yield call(queryAuthor, payload);
			if (response.status === 'SUCCESS') {
				yield put({
					type: 'setAuthor',
					payload: Array.isArray(response.data)
						? response.data
						: []
				});
			}
		},
		*fetch({
			payload
		}, {call, put}) {
			const response = yield call(queryArticleList, payload);
			if (response.status === 'SUCCESS') {
				yield put({type: 'queryList', payload: response.data});
			}
		},
		*pubArticle({
			payload
		}, {call, put}) {
			const response = yield call(pubArticle, payload);
			if (response.status === 'SUCCESS') {
				const newpayload = {
					article_id: payload.article_id,
					online: 1
				};
				yield put({
					type: 'resetList',
					payload: {
						...newpayload
					}
				});
				message.success('提交成功');
			} else {
				message.error(response.failedMsg);
			}
		},
		*cancelPubArticle({
			payload
		}, {call, put}) {
			const response = yield call(cancelPubArticle, payload);
			if (response.status === 'SUCCESS') {
				const newpayload = {
					article_id: payload.article_id,
					online: 0
				};
				yield put({
					type: 'resetList',
					payload: {
						...newpayload
					}
				});
				message.success('提交成功');
			} else {
				message.error(response.failedMsg);
			}
		},
		*appendFetch({
			payload
		}, {call, put}) {
			const response = yield call(queryArticleList, payload);
			if (response.status === 'SUCCESS') {
				if (response.data.list.length === 0) {
					message.info('没有更多了');
				} else {
					yield put({type: 'appendList', payload: response.data});
				}
			}
		},
		*updateListData({
			payload
		}, {put}) {
			yield put({
				type: 'updateList',
				payload: Array.isArray(payload)
					? payload
					: []
			});
		},
		*submitStepForm({
			payload
		}, {call, put}) {
			const params = {};
			const newPayload = {
				...payload
			};
			for (const value in payload) {
				if (payload[value] !== 'null') {
					params[value] = payload[value];
				}
			}
			const response = yield call(fakeSubmitArticle, params);
			if (response.status === 'SUCCESS') {
				newPayload.id = response.data.article_id;
				yield put({type: 'saveStepFormData', payload: newPayload});
				message.success('保存成功');
				// yield put(routerRedux.push(`/article/pub-article/setCover?id=${response.data.article_id}`));
			} else {
				message.error(response.failedMsg);
			}
		},
		*fatchArticle({
			payload
		}, {call, put}) {
			const article = yield call(queryArticle, payload);
			if (article.status === 'SUCCESS') {
				yield put({type: 'saveStepFormData', payload: article.data});
			} else {
				message.error('获取文章失败');
			}
		},
		*subCheck({
			payload
		}, {call, put}) {
			const response = yield call(subCheck, payload);
			if (response.status === 'SUCCESS') {
				const newpayload = {
					article_id: payload.article_id,
					status: 2
				};
				yield put({
					type: 'resetList',
					payload: {
						...newpayload
					}
				});
				message.success('提交成功');
			} else {
				message.error(response.failedMsg);
			}
		},
		*check({
			payload
		}, {call, put}) {
			const response = yield call(check, payload);
			if (response.status === 'SUCCESS') {
				yield put({type: 'resetList', payload});
				message.success('提交成功');
			} else {
				message.error(response.failedMsg);
			}
		}
	},

	reducers: {
		setAuthor(state, action) {
			return {
				...state,
				authors: action.payload
			};
		},
		clearStepData() {
			return {
				step: {
					id: '',
					author: '',
					intro: '',
					title: '',
					content: '',
					cover: ''
				}
			}
		},
		saveStepFormData(state, {payload}) {
			return {
				...state,
				step: {
					...state.step,
					...payload
				}
			};
		},
		queryList(state, action) {
			return {
				...state,
				list: action.payload.list,
				pagination: action.payload.pagination
			};
		},
		updateList(state) {
			return {
				...state,
				list: state.list.concat([])
			};
		},
		appendList(state, action) {
			return {
				...state,
				list: state.list.concat(action.payload.list),
				pagination: action.payload.pagination
			};
		},
		resetList(state, action) {
			const newlist = state.list;
			for (let i = 0; i < newlist.length; i++) {
				if (newlist[i].id === Number(action.payload.article_id)) {
					if (action.payload.status !== undefined) {
						newlist[i].status = action.payload.status
					}
					if (action.payload.online !== undefined) {
						newlist[i].online = action.payload.online
					}
				}
			}
			return {
				...state,
				list: newlist.concat([])
			};
		}
	}
};
